﻿using MicroAmoy.Application.Busines.CustomerManage;
using MicroAmoy.Application.Code;
using MicroAmoy.Application.Entity.CustomerManage;
using MicroAmoy.Util.WeChat;
using Newtonsoft.Json;
using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace MicroAmoy.Application.Web.Areas.MobileWebManage.Controllers
{
    public class ProductController : WeChatControllerBase
    {
        private MAProductBLL maproductbll = new MAProductBLL();
        private TBOrderBLL tborderbll = new TBOrderBLL();
        //
        // GET: /MobileWebManage/Product/
        /// <summary>
        /// 降价提醒
        /// </summary>
        /// <returns></returns>

        public ActionResult ReducedCommodity(string url = "")
        {
            var model = GetSignature(url);
            return View(model);
        }

        /// <summary>
        /// 上架提醒
        /// </summary>
        /// <returns></returns>
        public ActionResult ShelfReminder(string url = "")
        {
            var model = GetSignature(url);
            return View(model);
        }
        /// <summary>
        /// 订单中心
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderCenter()
        {
            return View();
        }
        /// <summary>
        /// 会员中心
        /// </summary>
        /// <returns></returns>
        public ActionResult MemberCenter()
        {
            var model = GetCustomerRelatedInfo(WechatUser.UserId);
            return View(model);
        }
        [HttpPost]
        [AjaxOnly]
        public ActionResult RemoveForm2(List<CustomerRelatedDelMdl> models)
        {
            DeleteCustomerRelatedInfo(models);
            return Success("删除成功。");
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult RemoveForm(string keyValue)
        {
            maproductbll.RemoveForm(keyValue);
            return Success("删除成功。");
        }

        /// <summary>
        /// 保存表单（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, TBOrderEntity entity)
        {
            if (null != tborderbll.GetEntityByOrderId(entity.OrderId))
            {
                return Error("该订单已经存在，请勿重复添加！");
            }
            entity.CustomerId = WechatUser.UserId;
            entity.Status = 1;
            entity.Id = Guid.NewGuid().ToString();
            entity.CreateDate = DateTime.Now;
            tborderbll.SaveForm(keyValue, entity, false);
            return Success("操作成功。");
        }
        #region
        void DeleteCustomerRelatedInfo(List<CustomerRelatedDelMdl> models)
        {
            string[] keys1 = models.Where(i => i.Type == 1).Select(i => i.Key).ToArray();
            string[] keys2 = models.Where(i => i.Type == 2).Select(i => i.Key).ToArray();
            //maproductbll.RemoveForm(keys1);
            //tborderbll.RemoveForm(keys2);
        }

        CustomerRelatedMdl GetCustomerRelatedInfo(string customerId)
        {
            var para = new
            {
                CustomerId = customerId
            };
            var list = maproductbll.GetList(JsonConvert.SerializeObject(para));
            var list2 = tborderbll.GetList(JsonConvert.SerializeObject(para));
            return new CustomerRelatedMdl()
            {
                Orders = list2.ToList(),
                PriceProducts = list.Where(i => i.Type == 1).ToList(),
                ShelfProducts = list.Where(i => i.Type == 2).ToList()
            };
        }
        #endregion
        /// <summary>
        /// 获取签名
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public SignatureModel GetSignature(string url)
        {
            string appId = Util.WeChat.Comm.Config.APPKEY;
            SignatureModel model = new SignatureModel()
            {
                AppId = appId,
                NonceStr = JSSDKHelper.GetNoncestr(),
                TimesTamp = JSSDKHelper.GetTimestamp(),
                ThisUrl = string.IsNullOrEmpty(url.Trim()) ? Request.Url.ToString() : url
            };

            //string ticket = AccessTokenContainer.TryGetJsApiTicket(model.AppId, siteConfig.appsecret);

            var token = WeChatHelper.GetToken(appId, "71943817545f23e5ac1f69a41d2e8dcd");
            string ticket = WeChatHelper.GetjsapiTicketToken(token);
            JSSDKHelper jsHelper = new JSSDKHelper();
            //最后一个参数url，必须为当前的网址  
            var strSignature = JSSDKHelper.GetSignature(ticket, model.NonceStr, model.TimesTamp, model.ThisUrl);
            model.Signature = strSignature;

            //var url = "http://wx.xuanwuju.com/MobileWebManage/Product/ReducedCommodity";// "http://m.wanwushuo.com/Home/WeixinLuyin";
            //SignatureModel model = WeChatHelper.GetSignature(ticket, url);
            //model.AppId = appId;
            return model;
        }
    }
}
